/* wifi-kii, a tool to set a Wii's wifi key from SD/USB
   Copyright (C) 2010  smoere (smoere@gmail.com)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/
	
/* 	this project includes other peoples code. i'll try to leave thoses copyrightheaders intact
	
	UNSTABLE UNTILL THIS LINE IS REMOVED
*/


#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <ogc/isfs.h>

#include "netsetlayout.h"

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
netconfig_t netconfig;

void init(){
	// Initialise the video system
	VIDEO_Init();
	
	// This function initialises the attached controllers
	WPAD_Init();
	PAD_Init();
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);					

	// Obtain the preferred video mode from the system
	// This will correspond to the settings in the Wii menu
	rmode = VIDEO_GetPreferredMode(NULL);

	// Allocate memory for the display in the uncached region
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	
	// Initialise the console, required for printf
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
	
	// Set up the video registers with the chosen mode
	VIDEO_Configure(rmode);
	
	// Tell the video hardware where our display memory is
	VIDEO_SetNextFramebuffer(xfb);
	
	// Make the display visible
	VIDEO_SetBlack(FALSE);

	// Flush the video register changes to the hardware
	VIDEO_Flush();

	// Wait for Video setup to complete
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

	
	// The console understands VT terminal escape codes
	// This positions the cursor on row 2, column 0
	// we can use variables for this with format codes too
	// e.g. printf ("\x1b[%d;%dH", row, column );
	printf("\x1b[2;0H");
}


//---------------------------------------------------------------------------------
int main(int argc, char **argv){
//---------------------------------------------------------------------------------

	int file,retval;
	bool oksofar;
	
	init();
	oksofar = true;
	file = -1;
	retval = -1;
	
	printf("wifi-kii phase 1: read test\n");
	
	if(oksofar){
		retval = ISFS_Initialize();
		if (retval < 0){
			printf("\nError: ISFS_Initialize() returned %d\n", retval);
			oksofar = false;
		}
	}
	
	if(oksofar){
		file = ISFS_Open("/shared2/sys/net/02/config.dat", ISFS_OPEN_READ);
		if (file < 0){
			printf("\nError: ISFS_OpenFile() returned %d\n", file);
			oksofar = false;
		}
	}
	
	if(oksofar){
		retval = ISFS_Read(file, &netconfig, sizeof(netconfig_t));
		if (retval < 0){
			printf("\nError: ISFS_Read() returned %d\n", retval);
			oksofar = false;
		}
	}
	
	if(oksofar){
		retval = ISFS_Close(file);
		if (retval < 0){
			printf("\nError: ISFS_Close() returned %d\n", retval);
			oksofar = false;
		}
	}
	if(oksofar){
		retval = ISFS_Deinitialize();
		if (retval < 0){
			printf("\nError: ISFS_Deinitialize() returned %d\n", retval);
			oksofar = false;
		}
	}
	
	if(oksofar){
		printf("YAY! there were no errors encountered while reading the configfile.\n");
		printf("now lets see if the values we've read are right:\n");
		printf("the Wii ip of your first config should be: %d.%d.%d.%d\n", netconfig.connection[0].ip[0], 
			netconfig.connection[0].ip[1], netconfig.connection[0].ip[2], netconfig.connection[0].ip[3]); 
		printf("the Wii ip of your second config should be: %d.%d.%d.%d\n", netconfig.connection[1].ip[0], 
			netconfig.connection[1].ip[1], netconfig.connection[1].ip[2], netconfig.connection[1].ip[3]); 
	}
	
	if(!oksofar){
		printf("dang. something went wrong. please let me know all the above errors.");
	}
	
	fflush(stdout);
	
	while(1) {

		// Call WPAD_ScanPads each loop, this reads the latest controller states
		WPAD_ScanPads();

		// WPAD_ButtonsDown tells us which buttons were pressed in this loop
		// this is a "one shot" state which will not fire again until the button has been released
		u32 pressed = WPAD_ButtonsDown(0);

		// We return to the launcher application via exit
		if ( pressed & WPAD_BUTTON_HOME ) exit(0);

		// Wait for the next frame
		VIDEO_WaitVSync();
	}

	return 0;
}
